# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=tea
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.9.2
pkgrel=1
pkgdesc='A command line tool to interact with Gitea servers (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://gitea.com/gitea/tea'
msys2_references=(
  'archlinux: tea'
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-go" "${MINGW_PACKAGE_PREFIX}-cc" "git")
optdepends=("git: To interact with repositories")
options=('!strip')
source=("${url}/archive/v${pkgver}/$_realname-$pkgver.tar.gz")
sha256sums=('b5a944de8db7d5af4aa87e9640261c925f094d2b6d26c4faf2701773acab219b')

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  cp -r "${srcdir}/${_realname}" "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}" && mkdir build

  export GOOS=windows
  export GOROOT=${MINGW_PREFIX}/lib/go
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GO_LDFLAGS="-s -w"
  case "${CARCH}" in
    i686|x86_64)
      GOFLAGS+=" -buildmode=pie"
      ;;
  esac

  ${MINGW_PREFIX}/bin/go build -v \
    -trimpath \
    -mod=readonly \
    -modcacherw \
    -ldflags "-linkmode=external -compressdwarf=false -X main.Version=${pkgver}" \
    -o build \
    .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  install -Dm755 "build/tea.exe" "${pkgdir}${MINGW_PREFIX}/bin/tea.exe"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 README.md "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README.md"
  install -Dm644 contrib/autocomplete.sh "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/tea"
  install -Dm644 contrib/autocomplete.zsh "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_tea"
}
